import { getUserAll } from '@/api/user'
import { SellProcedures, getwalletInfo } from "@/api/public";
import { setLocal, getLocal, clearLocal } from '@/utils/localStorage'
import router from "@/router";

const getDefaultState = () => {
  return {
    user_info: {},
    wallet_info: {}
  }
}

const state = getDefaultState()

const mutations = {
  SET_USER_INFO: (state, user_info) => {
    state.user_info = user_info
  },
  SET_WALLET_INFO: (state, wallet_info) => {
    state.wallet_info = wallet_info
  },
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
    window.location.reload()
  },
  sell: (state, sell) => {
    state.sell = sell
  }
}

const actions = {
  getUserAll({ commit, state }) {
    return new Promise((resolve, reject) => {
      getUserAll().then(response => {
        if (response.data.success) {
          commit('SET_USER_INFO', response.data.data)
          // resolve()
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  getWallet({ commit, state }) {
    return new Promise((resolve, reject) => {
      getwalletInfo().then(response => {
        if (response.data.success) {
          commit('SET_WALLET_INFO', response.data.data)
          resolve()
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  resetToken({ commit }) {
    return new Promise(resolve => {
      clearLocal()
      commit('RESET_STATE')
      resolve()
    })
  },
  getsell({ commit, state }) {
    return new Promise((resolve, reject) => {
      SellProcedures().then(response => {
        console.log(response.data.data)
        if (response.data.success) {
          commit('sell', response.data.data)
          resolve()
        }
      }).catch(error => {
        reject(error)
      })
    })

  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
